// attBillCalc.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "attBillCalc.h"
#include "Resource.h"
#include "Richedit.h"
#include "commctrl.h"
#include <windows.h>
#include <Shellapi.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <io.h>
#include <iostream>
#include <fcntl.h>



// Global Variables:
HINSTANCE hInst;								// current instance
char *lpszFile = NULL;
BOOL fileFound = FALSE;
char *convStr = NULL;

//Function decls
BOOL CALLBACK mainDlgProc(HWND main_hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
void createConsole();
BOOL handleDroppedFile(WPARAM wParam);
BOOL splitBill();

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	LoadLibrary("riched20.dll");
	if ( strcmp(lpCmdLine, "-console") == 0 ){
		createConsole();
	}

	DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDLG), NULL, mainDlgProc);

	//free memory
	if (lpszFile != NULL) free (lpszFile);
	if (convStr != NULL) free (convStr);

	return 0;
}

BOOL CALLBACK mainDlgProc(HWND main_hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_INITDIALOG:
			printf("Hello, World\n");
		break;

		case WM_DROPFILES:
		{
			fileFound = handleDroppedFile(wParam);
			break;
		}
		case WM_COMMAND:
		{	switch(LOWORD(wParam)) 
			{
				case IDC_BUTHELP:
					MessageBox(NULL, "Help Message Placeholder", "ATT Bill Calculator Help", MB_OK | MB_ICONINFORMATION | MB_TASKMODAL);
				break;

				case IDC_BUTCALC:
				{
					if (!fileFound) {
						MessageBox(NULL, "Please drag and drop a bill to split first.", "Error", MB_OK | MB_ICONWARNING | MB_TASKMODAL);
						break;
					}

					convStr = (char*) malloc ( (strlen(lpszFile) + 75) * sizeof(char) );
					if (convStr == NULL) {
						MessageBox(NULL, "Insufficient Memory!", "Error", MB_OK | MB_ICONERROR | MB_TASKMODAL);
						//return FALSE;
						break;
					}
					sprintf(convStr, "python ..\\..\\pdfminer-20110515\\tools\\pdf2txt.py -p 3 %s >convertedBill.txt", lpszFile);
					system(convStr);
					splitBill();
				}
				break;

				case IDC_BUTEXIT:
					EndDialog(main_hwnd, 0);
				break;
			}
			break;
		}
		case WM_CLOSE:
			EndDialog(main_hwnd, 0);
			break;

		default:
			return FALSE;
	}
	return TRUE;
}


BOOL splitBill()
{
	HANDLE fBill = INVALID_HANDLE_VALUE;
	int i = 0;
	char *fBuf = NULL;
	LPDWORD bytesRead = NULL;
	DWORD fileSize = 0;
	BOOL retval = FALSE;

	fBill = CreateFile("convertedBill.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (fBill == INVALID_HANDLE_VALUE) {
		printf("\nUnable to open file : %d", GetLastError());
		MessageBox(NULL, "Unable to open converted file", "Error", MB_OK | MB_ICONERROR | MB_TASKMODAL);
		return FALSE;
	}
	fileSize = GetFileSize(fBill, NULL);

	bytesRead = (LPDWORD) malloc (sizeof(DWORD));
	if (bytesRead == NULL) {
		MessageBox(NULL, "Insufficient Memory!", "Error", MB_OK | MB_ICONERROR | MB_TASKMODAL);
		return FALSE;
	}

	fBuf = (char*) malloc (fileSize * sizeof(char));
	if (fBuf == NULL) {
		MessageBox(NULL, "Insufficient Memory!", "Error", MB_OK | MB_ICONERROR | MB_TASKMODAL);
		return FALSE;
	}
	retval = ReadFile(fBill, fBuf, fileSize, bytesRead, NULL);
	printf("%s\n\n", fBuf);

	free(fBuf);
	free(bytesRead);
	CloseHandle(fBill);
	return TRUE;
}


BOOL handleDroppedFile(WPARAM wParam) {
	int numFiles = 0;
	UINT cch = 0;
	HDROP hDrop;

	hDrop = (HDROP)wParam;
	numFiles = DragQueryFile (hDrop, 0xFFFFFFFF, (LPSTR)NULL, 0);
	if (numFiles > 1) {
		MessageBox(NULL, "You have dropped more than one file. This is not allowed.", "Error", MB_OK | MB_ICONWARNING | MB_TASKMODAL);
		return FALSE;
	}
	cch = DragQueryFile (hDrop, 0, lpszFile, (cch+1));    //This gets no of chars required
	lpszFile = (char*) malloc ( (cch+1) * sizeof(char) );
	if (lpszFile == NULL) {
		MessageBox(NULL, "Insufficient Memory!", "Error", MB_OK | MB_ICONERROR | MB_TASKMODAL);
		return FALSE;
	}
	DragQueryFile (hDrop, 0, lpszFile, (cch+1));          //This copies filename into malloc'd buffer
	printf("File: %s\n", lpszFile);
	fileFound = TRUE;
	return TRUE;
}


void createConsole() {
	
	int hConHandle;	
	long lStdHandle;	
	CONSOLE_SCREEN_BUFFER_INFO coninfo;	
	FILE *fp;	
	const unsigned int MAX_CONSOLE_LINES = 500;	// set the screen buffer to be big enough to let us scroll text	

	AllocConsole();	
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),	&coninfo);	
	coninfo.dwSize.Y = MAX_CONSOLE_LINES;	
	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),	coninfo.dwSize);	// redirect unbuffered STDOUT to the console	
	lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);	
	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);	
	fp = _fdopen( hConHandle, "w" );	
	*stdout = *fp;	setvbuf( stdout, NULL, _IONBF, 0 );	// redirect unbuffered STDIN to the console	
	lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);	
	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);	
	fp = _fdopen( hConHandle, "r" );	
	*stdin = *fp;	
	setvbuf( stdin, NULL, _IONBF, 0 );	// redirect unbuffered STDERR to the console	
	lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);	
	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);	
	fp = _fdopen( hConHandle, "w" );	
	*stderr = *fp;	
	setvbuf( stderr, NULL, _IONBF, 0 );	// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog	// point to console as well	
	std::ios::sync_with_stdio();
}
